********************************************************************************
*     REPLICATION OF THE MAIN RESULTS 
*     Do wars abroad affect attitudes at home? (2024) PNAS Nexus, 3(8)
*	  Klymak, M., & Vlandas, T. 
********************************************************************************

      version 18
      clear all
      set more off
      use "data.dta", clear
      set scheme s1manual
	 
********************************************************************************
*   DATA PREPARATION
********************************************************************************

      g d7=1 if distance_invasion<=7
      replace d7=0 if distance_invasion<0
      label var d7 "7 days"

      g d14=1 if distance_invasion<=14
      replace d14=0 if distance_invasion<0
      label var d14 "14 days"

      g d21=1 if distance_invasion<=21
      replace d21=0 if distance_invasion<0
      label var d21 "21 days"

      g d28=1 if distance_invasion<=28
      replace d28=0 if distance_invasion<0
      label var d28 "28 days"

      g d31=1 if distance_invasion<=31
      replace d31=0 if distance_invasion<0
      label var d31 "31 days"

      g d40=1 if distance_invasion<=40
      replace d40=0 if distance_invasion<0
      label var d40 "40 days"

      g d50=1 if distance_invasion<=50
      replace d50=0 if distance_invasion<0
      label var d50 "50 days"

      g d60=1 if distance_invasion<=60
      replace d60=0 if distance_invasion<0
      label var d60 "60 days"

********************************************************************************
*   FIGURES 1 AND 2
********************************************************************************

      local control_vars education age female difficultonincome ///
      i.sourceincome bornincountry urban i.id 

      estimates drop _all

      foreach var of varlist atcherp leaveeu becomeeu euftf implvdm gincdif accalaw loylead imbgeco imueclt imdfetn impcntr {

      quietly: ebalance d7 `control_vars' if distance_invasion<=7 , tar(2) basewt(dweight) 
      eststo d7: quietly reg `var' d7 `control_vars' [pweigh=_webal] if distance_invasion<=7, vce(robust)
      margins, dydx(d7)
      est store d7

      quietly: ebalance d14 `control_vars' if distance_invasion<=14 , tar(2) basewt(dweight) 
      eststo d14: quietly reg `var' d14 `control_vars' [pweigh=_webal] if distance_invasion<=14, vce(robust)
      margins, dydx(d14)
      est store d14

      quietly: ebalance d21 `control_vars' if distance_invasion<=21, tar(2) basewt(dweight) 
      eststo d21: quietly reg `var' d21 `control_vars' [pweigh=_webal] if distance_invasion<=21, vce(robust)
      margins, dydx(d21)
      est store d21

      quietly: ebalance d28 `control_vars' if distance_invasion<=28, tar(2) basewt(dweight) 
      eststo d28: quietly reg `var' d28 `control_vars' [pweigh=_webal] if distance_invasion<=28, vce(robust)
      margins, dydx(d28)
      est store d28

      quietly: ebalance d31 `control_vars' if distance_invasion<=31, tar(2) basewt(dweight) 
      eststo d31: quietly reg `var' d31 `control_vars' [pweigh=_webal] if distance_invasion<=31, vce(robust)
      margins, dydx(d31)
      est store d31

      quietly: ebalance d40 `control_vars' if distance_invasion<=40, tar(2) basewt(dweight) 
      eststo d40: quietly reg `var' d40 `control_vars' [pweigh=_webal] if distance_invasion<=40, vce(robust)
      margins, dydx(d40)
      est store d40

      quietly: ebalance d50 `control_vars' if distance_invasion<=50, tar(2) basewt(dweight) 
      eststo d50: quietly reg `var' d50 `control_vars' [pweigh=_webal] if distance_invasion<=50, vce(robust)
      margins, dydx(d50)
      est store d50

      eststo d60: quietly ebalance d60 `control_vars' if distance_invasion<=60, tar(2) basewt(dweight) 
      eststo d60: quietly reg `var' d60 `control_vars' [pweigh=_webal] if distance_invasion<=60, vce(robust)
      margins, dydx(d60)
      est store d60 
  
      coefplot d7 d14 d21 d28 d31 d40 d50 d60, /// 
      drop(_cons) keep(d7 d14 d21 d28 d31 d40 d50 d60) ///
      yline(0, lcolor(gs8)) msymbol(c) levels(90) ///
      vertical ciopts(recast(. rcap)) ///
      lwidth(thick) legend(off) msize(medlarge) xlabel(, labsize(medium)) ///
      pstyle(p1) color(black%100) graphregion(margin(l-5)) ///
      offset(+.025) coeflabels(d7="7 days" d14="14 days" d21="21 days" ///
      d28="28 days" d31="31 days" d40="40 days" d50="50 days" d60="60 days") ///
      ylabel(, labsize(medium)) ytitle("Coefficient") xtitle("Days", size(medium)) name(x, replace)    

      graph export coefplt_`var'.pdf, as(pdf) replace
}
